﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.IO;

namespace DLL.Courseware
{
	/// <summary>
	/// 视图控制类
	/// </summary>
	/// <![CDATA[2014-8-7 17:52:59]]>
	public class View : DLL.ShowLayer.FormView
	{
		private Component clsthisComp;

		#region 添加、修改
		private DropDownList ddlCoursewareSort = new DropDownList();
		private Brettle.Web.NeatUpload.InputFile inputFile = new Brettle.Web.NeatUpload.InputFile();
		private Brettle.Web.NeatUpload.ProgressBar pBar = new Brettle.Web.NeatUpload.ProgressBar();
		private FileUpload fUpload = new FileUpload();
		#endregion 添加、修改

		#region 查询

		#endregion 查询

		#region 浏览

		#endregion 浏览

		#region 构造函数
		/// <summary>
		/// 视图类构造函数
		/// </summary>
		/// <![CDATA[2014-8-23 11:54:15]]>
		public View(System.Web.UI.Page page)
			: base(page)
		{
			clsthisComp = new Component(page);

			clsComp = clsthisComp;

			clsControls.ShowCoursewareSort(ddlCoursewareSort);
			pBar.Inline = true;
			pBar.Visible = false;
			//listAddEdit.Add(new TonyNet4V1.Structs.TitleControlEntity("类型", "DropDownList", ddlCoursewareSort, "int", "nSortID"));
		}
		#endregion 构造函数

		#region 类型转换

		protected override TonyNet4V1.EntityBase GetEntity()
		{
			return new Entity();
		}

		protected override TonyNet4V1.QueryEntityBase GetQuery()
		{
			return new QueryEntity();
		}

		protected override TonyNet4V1.ComponentBase GetComponent()
		{
			return clsthisComp;
		}

		#endregion 类型转换

		public override void AddEditLayout(Panel pPanel)
		{
			//base.AddEditLayout(pPanel);
			//pPanel.Controls.Add(radPM);
			//pPanel.Controls.Add(radPA);
			//pPanel.Controls.Add(fUpload);

			Table tableAdd = new Table();

			TableRow tRow1 = new TableRow();
			TableCell tCell11 = new TableCell();
			TableCell tCell12 = new TableCell();

			TableRow tRow2 = new TableRow();
			TableCell tCell21 = new TableCell();
			TableCell tCell22 = new TableCell();

			tCell11.Text = "课件类型";
			tCell12.Controls.Add(ddlCoursewareSort);

			tCell21.Text = "课件";
			tCell22.Controls.Add(inputFile);

			tCell11.CssClass = tableCellOne.CssClass;
			tCell12.CssClass = tableCellTwo.CssClass;
			tCell21.CssClass = tableCellOne.CssClass;
			tCell22.CssClass = tableCellTwo.CssClass;

			tRow1.CssClass = tableRow.CssClass;
			tRow2.CssClass = tableRow.CssClass;

			tableAdd.CssClass = table.CssClass;

			tRow1.Controls.Add(tCell11);
			tRow1.Controls.Add(tCell12);
			tRow2.Controls.Add(tCell21);
			tRow2.Controls.Add(tCell22);
			tableAdd.Controls.Add(tRow1);
			tableAdd.Controls.Add(tRow2);

			pPanel.Controls.Add(tableAdd);
			//pPanel.Controls.Add(inputFile);
			//pPanel.Controls.Add(pBar);
		}

		public override bool Add()
		{
			//return base.Add();
			//foreach (Telerik.WebControls.UploadedFile file in Telerik.WebControls.RadUploadContext.Current.UploadedFiles)
			//{
			//        string Path = Server.MapPath("~/upload");

			//        if (System.IO.Directory.Exists(Path) == false)
			//        {
			//                System.IO.Directory.CreateDirectory(Path);
			//        }

			//        Path = Path + "/" + DateTime.Now.ToString("yyyyMMdd") + "." + file.GetName();

			//        file.SaveAs(Path, true);
			//}

			//pBar.Visible = true;

			string strVirtualFolder = "~/UploadFiles/Courseware/" + TonyNet4V1.CommonFunction.GetNowYearMonth() + "/";
			FileInfo fi = new FileInfo(inputFile.FileName);
			string folder = System.Web.HttpContext.Current.Server.MapPath(strVirtualFolder);
			string saveFileName = string.Format("{0}{1:yyMMdd-HHmmss}-{2,3}{3}",
			    folder,
			    DateTime.Now,
			    new Random().Next(0, 999),
			    fi.Extension
			    );

			string strConvertFileName = TonyNet4V1.CommonFunction.GetNowDateTime() + fi.Extension;

			saveFileName = folder + strConvertFileName;

			if (!Directory.Exists(folder))
				Directory.CreateDirectory(folder);
			if (inputFile.ContentLength > 0)
				inputFile.MoveTo(saveFileName, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);

			Entity clsAddCourseware = new Entity();

			clsAddCourseware.DepartmentID = nDepartmentID;
			clsAddCourseware.Name = inputFile.FileName;
			clsAddCourseware.Path = strVirtualFolder + strConvertFileName;
			clsAddCourseware.SortID = Convert.ToInt32(ddlCoursewareSort.Text);
			clsAddCourseware.Type = inputFile.ContentType;
			clsAddCourseware.UserID = nUserID;
			clsAddCourseware.Public = true;

			if (clsthisComp.Add(clsAddCourseware))
			{
				TonyNet4V1.ShowMessageAlert.ShowMessage(clsPage, "上传课件成功！");
			}

			return true;
		}
	}
}
